<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-03-22 14:35:48
 * @Description: 洗车单
 */
 
class CarWashOrder extends Eloquent
{
  protected $table = "car_wash_order";
  protected $primaryKey = 'order_id';
  // public static $product = array(
  //     "39"=>"洗车(suv)",
  //     "38"=>"洗车(轿车)",      
  // );
	public function maintenanceShops(){
		return $this->hasOne('MaintenanceShops','id','shop_id');
	}
   /**
    * @Author:      lyt
    * @DateTime:  2017-03-23 13:46:31
    * @Description: 订单记录 收入
    */
   	public static function pay($order_id,$pay_type,$sc_user_id,$service_card_detail_pack_id){
      	return DB::transaction(function () use ($order_id,$pay_type,$sc_user_id,$service_card_detail_pack_id) {
	        $order = CarWashOrder::where("order_id",$order_id)->lockForUpdate()->first();
	        $order_amount=$order->amount-$order->dis_amount;
	        // $tran_no = date('Ymd',time()).Common::generateCode();
          $tran_no = date('YmdHis',time()).Common::generateCode(8);
	        // if($pay_type== 1||$pay_type== 7){//现金 或者银行卡支付            
	        //     Account::record_base('1',$tran_no,1,'0106',$order_amount,'洗车收入');
	        //     //记录订单账务关系
	        //     CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
	        // }     
	        $user = Users::find($order->user_id);
	        $account = UserAccount::where('user_id',$order->user_id)->first();
	        $o_p=new CarWashOrderPayRecord;
	        $o_p->order_id=$order_id;
	        $o_p->pay_type=$pay_type;
	        $o_p->amount=$order_amount;
	        $o_p->status=1;
	        $o_p->create_by=$sc_user_id;
	        $o_p->tran_no=$tran_no;
	        $o_p->create_type="sc";
          $o_p->implement_time=date("Y-m-d H:i:s",time());
	        $o_p->save();


	        if($pay_type== 1||$pay_type== 2||$pay_type== 3||$pay_type== 7){//现金 或者银行卡支付
                

                //记录用户充值 不记录充值记录  只记录流水
                Account::record($order->user_id,$order->shop_id,1,$tran_no,1,106,$order_amount,'洗车单结算充值,洗车单号:'.$order->order_id);
                //记录用户支出
                Account::record($order->user_id,$order->shop_id,1,$tran_no,2,206,$order_amount,'洗车单结算支出,洗车单号:'.$order->order_id);
                //记录公司账户收入
                Account::record_base('1',$tran_no,1,106,$order_amount,'洗车单结算收入,洗车单号:'.$order->order_id);

                //记录订单账务关系
                CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
          }
          if($pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空 新版本判断用户余额 个人用用户 授信额为0        
          	$uad_balance=UserAccountDeatails::where("user_id",$user->id)->whereIn("acount_type",array(1,2))->sum("balance");      
            	if($uad_balance >= $order_amount){
                	$arr=AcountUserRecharge::getUseRecharge($order->shop_id,$order->user_id,$order_amount,2,"asc");
                	if(sizeof($arr)>0){
                  	foreach ($arr as $_arr) {
                    	//记录了用户的充值消费记录
                    	$aurs=new AcountUserRechargeSpend;
                    	$aurs->aurr_id=$_arr->id;
                    	$aurs->user_id=$order->user_id;
                    	$aurs->buss_id=$o_p->id;
                    	$aurs->type=2;//记录洗车单消费
                    	$aurs->amount=$_arr->user_amount;
                    	$aurs->status=1;
                    	$aurs->tran_no=$tran_no;
                    	$aurs->shop_id=$order->shop_id;
                    	$aurs->implement_time=date("Y-m-d H:i:s",time());
                    	$aurs->save();
                    	//更改充值记录消费的记录
                    	AcountUserRecharge::where('id',$_arr->id)->update(array('current_amount'=> $_arr->current_amount -$_arr->user_amount,));
                    	//记录个人 各种账户余额
                    	Account::record($order->user_id,$_arr->shop_id,$_arr->acount_type,$tran_no,2,206,$_arr->user_amount,'洗车单结算支出,洗车单号:'.$order->order_id);
                    	//记录公司账户收入
                    	$base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                    	Account::record_base($base_acount_num,$tran_no,1,106,$_arr->user_amount,'洗车单结算收入,洗车单号:'.$order->order_id);
                  }
                  CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
              }else{
                  DB::rollback();
                  $response = Lang::get("scs_error.order_user_notpay_40039");
                  return $response;
              }                  
              }else{
                  DB::rollback();
                  $response = Lang::get("scs_error.order_user_notpay_40039");
                  return $response;
              }
          }

          if($user->type ==2&&$pay_type==6){//企业挂账
              // $account = UserAccount::where('user_id',$order->user_id)->first();
              $c=Companys::find($user->id);
              $check=Companys::accountDayStatus($c);
              if($check["day_status"]=='逾期（禁止挂帐）'){
                DB::rollback();
                $response = Lang::get("scs_error.order_user_notpay_40111");
                return $response;
              }
              $uad=UserAccountDeatails::where("user_id",$user->id)->where("acount_type",3)->first();
              if(($uad->balance + $uad->credit) >= $order_amount){

                //挂账 只记录用户虚拟账户支出 挂账收回时候 记录平台收入  挂账退货 没收回退货会用户帐 ，收回之后退货，记录不回用户帐记录企业支出
                //挂账账户是 平台门店传 0
                Account::record($order->user_id,0,3,$tran_no,2,206,$order_amount,'订单结算支出,洗车单号:'.$order->order_id);

                CarWashOrderAccount::insert(array('order_id' => $order->order_id,'tran_no' => $tran_no,));
              }else{
                  DB::rollback();
                  $response = Lang::get("scs_error.order_user_notpay_40039");
                  return $response;
              }
          }

          if($pay_type==50){  
            $scdp=ServiceCardDetailPack::find($service_card_detail_pack_id);
             //记录订单使用套餐卡记录
            $scc=new ServiceCardConsumption();
            $scc->order_id=$order_id;
            $scc->service_card_detail_pack_id=$scdp->id;
            $scc->service_card_detail_id=$scdp->service_card_detail_id;
            $scc->type=1;
            $scc->product_id=0;
            $scc->status=1;
            $scc->save();
            //减少套餐卡ID
             DB::table("service_card_detail_pack")->where("id",$scdp->id)->decrement("curr_number");
             //如果是是线上 套餐卡支付  发送短信和微信
             $temp_shop=MaintenanceShops::find($order->shop_id);
             Users::checkServiceCardPack($order->order_id,$order->user_id,array(array("pack_id"=>$scdp->pack_id,"num"=>1)),$order->car_no,$temp_shop->name,date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff")),0);
          } 

          //积分返还
          if(in_array($pay_type,CarWashOrderPayRecord::$ei_pay_type)){//说明是返回积分的支付类型
            $mc=MemberCoefficient::where("source",4)->where("status",1)->first();
            $ms=MaintenanceShops::find($order->shop_id);
            UserIntegralFlow::record($order->user_id,$order->shop_id,$order->order_id,1,1004,floor($order_amount*$mc->integral*$ms->integral/100),'洗车订单完成积分');
            UserExperienceFlow::record($order->user_id,$order->shop_id,$order->order_id,1,1004,floor($order_amount*$mc->experience*$ms->experience/100),'洗车订单完成经验');
            Users::checkMember($order->user_id);
          }


	        $order->pay_amount=$order_amount;
	        $order->pay_type=$pay_type;
	        $order->status=1417;
	        $order->pay_time=date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff"));
	        $order->save();
	        return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order->order_id));
      });

    }

}